home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWBitmap.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.0 KB  |  479 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWBitmap.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWBITMAP_H
  13. #include "FWBitmap.h"
  14. #endif
  15.  
  16. #ifndef SLBITMAP_H
  17. #include "SLBitmap.h"
  18. #endif
  19.  
  20. #ifndef SLGRGLOB_H
  21. #include "SLGrGlob.h"
  22. #endif
  23.  
  24. #ifndef FWMEMHLP_H
  25. #include "FWMemHlp.h"
  26. #endif
  27.  
  28. #ifndef FWCOLOR_H
  29. #include "FWColor.h"
  30. #endif
  31.  
  32. #ifndef FWGC_H
  33. #include "FWGC.h"
  34. #endif
  35.  
  36. #ifndef FWACQUIR_H
  37. #include "FWAcquir.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. #ifndef FWGRUTIL_H
  45. #include "FWGrUtil.h"
  46. #endif
  47.  
  48. #ifndef FWWINRES_H
  49. #include "FWWinRes.h"
  50. #endif
  51.  
  52. #ifndef FWPALETE_H
  53. #include "FWPalete.h"
  54. #endif
  55.  
  56. #ifndef SLWINDIB_H
  57. #include "FWWinDIB.h"
  58. #endif
  59.  
  60. #ifndef FWSOMENV_H
  61. #include "FWSOMEnv.h"
  62. #endif
  63.  
  64. #if defined(FW_BUILD_WIN) && !defined(FWGRUTIL_H)
  65. #include "FWGrUtil.h"
  66. #endif
  67.  
  68. #if defined(FW_BUILD_MAC) && !defined(FWRESACC_H)
  69. #include "FWResAcc.h"
  70. #endif
  71.  
  72. #if defined(FW_BUILD_MAC) && !defined(FWPICTUR_H)
  73. #include "FWPictur.h"
  74. #endif
  75.  
  76. #if defined(FW_BUILD_MAC) && !defined(FWRESSIN_H)
  77. #include "FWResSin.h"
  78. #endif
  79.  
  80. #if defined(FW_BUILD_MAC) && !defined(__PICTUTILS__)
  81. #include "PictUtils.h"
  82. #endif
  83.  
  84. // ----- Foundation Includes -----
  85.  
  86. #ifndef FWSTREAM_H
  87. #include "FWStream.h"
  88. #endif
  89.  
  90. #ifdef FW_BUILD_MAC
  91. #pragma segment FWGraphics_Bitmap
  92. #endif
  93.  
  94. //========================================================================================
  95. //    Template instantiation
  96. //========================================================================================
  97.  
  98. #include "FWGrRef.tpp"
  99.  
  100. FW_DEFINE_AUTO_TEMPLATE(FW_TGrRefPtr, FW_HBitmap)
  101.  
  102. #ifdef FW_USE_TEMPLATE_PRAGMAS
  103.  
  104. #pragma template_access public
  105. #pragma template FW_TGrRefPtr<FW_HBitmap>
  106.  
  107. #else
  108.  
  109. template class FW_TGrRefPtr<FW_HBitmap>;
  110.  
  111. #endif
  112.  
  113. //========================================================================================
  114. //    class FW_CBitmap
  115. //========================================================================================
  116.  
  117. FW_DEFINE_AUTO(FW_CBitmap)
  118.     
  119. //----------------------------------------------------------------------------------------
  120. //    FW_PrivAcquireGrRep
  121. //----------------------------------------------------------------------------------------
  122.  
  123. void FW_PrivAcquireGrRep(FW_HBitmap rep)
  124. {
  125.     if (rep != 0)
  126.         FW_PrivBitmap_Acquire(rep);
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. //    FW_PrivReleaseGrRep
  131. //----------------------------------------------------------------------------------------
  132.  
  133. void FW_PrivReleaseGrRep(FW_HBitmap rep)
  134. {
  135.     if (rep != 0)
  136.         FW_PrivBitmap_Release(rep);
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. //    FW_CBitmap::FW_CBitmap
  141. //----------------------------------------------------------------------------------------
  142.  
  143. FW_CBitmap::FW_CBitmap()
  144. {
  145.     FW_END_CONSTRUCTOR
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. //    FW_ValidatePixelSize
  150. //----------------------------------------------------------------------------------------
  151.  
  152. static short FW_ValidatePixelSize(short pixelSize)
  153. {
  154. #ifdef FW_BUILD_MAC
  155.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  156.               pixelSize == 8 || pixelSize == 16 || pixelSize == 24 || pixelSize == 32);
  157.     if (pixelSize == 24)
  158.         pixelSize = 32;
  159. #endif
  160. #ifdef FW_BUILD_WIN
  161.     FW_ASSERT(pixelSize == 0 || pixelSize == 1 || pixelSize == 2 || pixelSize == 4 ||
  162.               pixelSize == 8 || pixelSize == 16 || pixelSize == 24 || pixelSize == 32);
  163.     if (pixelSize == 32)
  164.         pixelSize = 24;
  165. #endif
  166.  
  167.     return pixelSize;
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. //    FW_CBitmap::FW_CBitmap
  172. //----------------------------------------------------------------------------------------
  173.  
  174. FW_CBitmap::FW_CBitmap(short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
  175. {
  176.     pixelSize = FW_ValidatePixelSize(pixelSize);
  177.  
  178.     FW_PlatformError error;
  179.     FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
  180.                         NULL, 0, 0, &error);
  181.     FW_FailOnError(error);
  182.     SetRep(rep);
  183.     FW_END_CONSTRUCTOR
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. //    FW_CBitmap::FW_CBitmap
  188. //----------------------------------------------------------------------------------------
  189.  
  190. FW_CBitmap::FW_CBitmap(void* image, long imageSize, short rowBytes,
  191.                        short width, short height, short pixelSize, FW_Palette palette /* = NULL */)
  192. {
  193.     FW_ASSERT(image != 0);
  194.  
  195.     pixelSize = FW_ValidatePixelSize(pixelSize);
  196.  
  197.     FW_PlatformError error;
  198.     FW_HBitmap rep = FW_PrivBitmap_CreateFromBits(width, height, pixelSize, palette,
  199.                         image, imageSize, rowBytes, &error);
  200.     FW_FailOnError(error);
  201.     SetRep(rep);
  202.     FW_END_CONSTRUCTOR
  203. }
  204.  
  205. #ifdef FW_BUILD_MAC
  206.  
  207. const FW_SRect FW_CBitmap::gWholePicture    =    {    0, 0, 0, 0    };
  208.  
  209. //----------------------------------------------------------------------------------------
  210. //    FW_CBitmap::FW_CBitmap
  211. //----------------------------------------------------------------------------------------
  212.  
  213. FW_CBitmap::FW_CBitmap(const FW_CPicture& picture, const FW_CColor& fillColor, const FW_CRect& pictPart)
  214. {
  215.     FW_CRect bounds = pictPart;
  216.  
  217. #if 0
  218.     if(&pictPart == &gWholePicture)
  219.         picture.GetPictBounds(bounds);
  220. #else
  221.     if(pictPart == gWholePicture)
  222.         picture.GetPictBounds(bounds);
  223. #endif
  224.     
  225.     FW_PlatformError error;
  226.     FW_HBitmap rep = FW_PrivBitmap_MacCreateFromPicture(picture, fillColor, bounds, &error);
  227.     FW_FailOnError(error);
  228.     SetRep(rep);
  229.     FW_END_CONSTRUCTOR
  230. }
  231.  
  232. #endif
  233.  
  234. #ifdef FW_BUILD_MAC
  235.  
  236. //----------------------------------------------------------------------------------------
  237. //    FW_CBitmap::MacGetAsPicture
  238. //----------------------------------------------------------------------------------------
  239.  
  240. FW_CPicture FW_CBitmap::MacGetAsPicture(const FW_SRect& pictPart) const
  241. {
  242.     FW_CRect bounds = pictPart;
  243. #if 0
  244.     if(&pictPart == &gWholePicture)
  245.         GetBitmapBounds(bounds);
  246. #else
  247.     if (FW_CRect(gWholePicture)==pictPart)
  248.         GetBitmapBounds(bounds);
  249. #endif
  250.  
  251.     FW_PlatformError error;
  252.     FW_HPicture hPict = FW_PrivBitmap_MacGetAsPicture(fRep, bounds, &error);
  253.     FW_FailOnError(error);
  254.  
  255.     FW_CPicture pict;
  256.     pict.SetRep(hPict);
  257.     return pict;
  258. }
  259.  
  260. #endif
  261.  
  262. //----------------------------------------------------------------------------------------
  263. //    FW_CBitmap::FW_CBitmap
  264. //----------------------------------------------------------------------------------------
  265.  
  266. FW_CBitmap::FW_CBitmap(FW_PlatformBitmap platformBitmap)
  267. {
  268.     FW_PlatformError error;
  269.     FW_HBitmap rep = FW_PrivBitmap_CreateFromPlatformBitmap(platformBitmap, &error);
  270.     FW_FailOnError(error);
  271.     SetRep(rep);
  272.     FW_END_CONSTRUCTOR
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. //    FW_CBitmap::FW_CBitmap
  277. //----------------------------------------------------------------------------------------
  278.  
  279. FW_CBitmap::FW_CBitmap(FW_PResourceFile& resourceFile, FW_ResourceID resID)
  280. {
  281.     FW_PlatformError error;
  282.     FW_HBitmap rep = FW_PrivBitmap_CreateFromResource(resourceFile, resID, &error);
  283.     FW_FailOnError(error);
  284.     SetRep(rep);
  285.     FW_END_CONSTRUCTOR
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. //    FW_CBitmap::FW_CBitmap
  290. //----------------------------------------------------------------------------------------
  291.  
  292. FW_CBitmap::FW_CBitmap(FW_CReadableStream& stream, FW_Boolean bDIBHeader)
  293. {
  294.     FW_PlatformError error;
  295.     FW_HBitmap rep = FW_PrivBitmap_Read(stream, bDIBHeader, &error);
  296.     FW_FailOnError(error);
  297.     SetRep(rep);
  298.     FW_END_CONSTRUCTOR
  299. }
  300.  
  301. //----------------------------------------------------------------------------------------
  302. //    FW_CBitmap::FW_CBitmap
  303. //----------------------------------------------------------------------------------------
  304.  
  305. FW_CBitmap::FW_CBitmap(const FW_CBitmap& other) :
  306.     FW_TGrRefPtr<FW_HBitmap>(other)
  307. {
  308.     FW_END_CONSTRUCTOR
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. //    FW_CBitmap::FW_CBitmap
  313. //----------------------------------------------------------------------------------------
  314.  
  315. FW_CBitmap::~FW_CBitmap()
  316. {
  317.     FW_START_DESTRUCTOR
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. //    FW_CBitmap::operator=
  322. //----------------------------------------------------------------------------------------
  323.  
  324. FW_CBitmap& FW_CBitmap::operator=(const FW_CBitmap& other)
  325. {
  326.     FW_TGrRefPtr<FW_HBitmap>::operator=(other);
  327.     return *this;
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. //    FW_CBitmap::Copy
  332. //----------------------------------------------------------------------------------------
  333.  
  334. FW_CBitmap FW_CBitmap::Copy(const FW_CRect& srcRect) const
  335. {
  336.     FW_PlatformError error;
  337.     FW_HBitmap rep = FW_PrivBitmap_Copy(fRep, srcRect, &error);
  338.     FW_FailOnError(error);
  339.     
  340.     FW_CBitmap bitmap;
  341.     bitmap.SetRep(rep);
  342.  
  343.     return bitmap;
  344. }
  345.  
  346. //----------------------------------------------------------------------------------------
  347. //    FW_CBitmap::GetPalette
  348. //----------------------------------------------------------------------------------------
  349.  
  350. FW_Palette FW_CBitmap::GetPalette() const
  351.     FW_PlatformError error;
  352.     FW_Palette palette = FW_PrivBitmap_GetPalette(fRep, &error);
  353.     FW_FailOnError(error);
  354.     return palette;
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. //    FW_CBitmap::GetBitmapBounds
  359. //----------------------------------------------------------------------------------------
  360.  
  361. void FW_CBitmap::GetBitmapBounds(FW_SGraphicContext& gc, FW_SRect& bounds) const
  362.     FW_PrivBitmap_GetBitmapBoundsGC(gc.fEnvironment, fRep, gc, bounds); 
  363.     FW_FailOnEvError(gc.fEnvironment);
  364. }
  365.         
  366. //========================================================================================
  367. //    Stream I/O
  368. //========================================================================================
  369.  
  370. //----------------------------------------------------------------------------------------
  371. //    operator>>
  372. //----------------------------------------------------------------------------------------
  373.  
  374. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CBitmap& bitmap)
  375. {
  376.     FW_PlatformError error;
  377.     FW_HBitmap rep = FW_PrivBitmap_Read(stream, TRUE, &error);
  378.     FW_FailOnError(error);
  379.     bitmap.SetRep(rep);
  380.     return stream;
  381. }
  382.  
  383. //----------------------------------------------------------------------------------------
  384. //    operator<<
  385. //----------------------------------------------------------------------------------------
  386.  
  387. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CBitmap& bitmap)
  388. {
  389.     FW_PlatformError error;
  390.     FW_PrivBitmap_Write(bitmap.fRep, stream, TRUE, &error);
  391.     FW_FailOnError(error);
  392.     return stream;
  393. }
  394.  
  395. //========================================================================================
  396. //    class FW_CBitmapContext
  397. //========================================================================================
  398.  
  399. FW_DEFINE_AUTO(FW_CBitmapContext)
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //    FW_CBitmapContext::FW_CBitmapContext
  403. //----------------------------------------------------------------------------------------
  404.  
  405. FW_CBitmapContext::FW_CBitmapContext(Environment* ev, FW_CBitmap& bitmap) :
  406.     FW_CGraphicContext(ev),
  407.     fBitmap(bitmap),
  408.     fGraphicDevice(NULL)
  409. {
  410.     FW_ASSERT(bitmap != NULL);
  411.     FW_PlatformBitmap    platformBitmap = bitmap.GetPlatformBitmap();
  412.     ODPlatformCanvas    platformCanvas;
  413.  
  414. #ifdef FW_BUILD_WIN
  415.     platformCanvas = ::CreateCompatibleDC(NULL);
  416.     fOldBitmap = (HBITMAP) ::SelectObject(platformCanvas, platformBitmap);
  417.     
  418.     if(fOldBitmap == NULL)
  419.         FW_Failure(FW_xGraphicException);
  420. #endif
  421. #ifdef FW_BUILD_MAC
  422.     platformCanvas = (ODPlatformCanvas) platformBitmap;
  423.     bitmap.MacLockPixels();
  424. #endif
  425.     fGraphicDevice = FW_PrivGDev_CreateFromPlatformCanvas(ev, platformCanvas);
  426.     FW_FailOnEvError(ev);
  427.     FW_PrivGDev_SetResolution(fGraphicDevice, FW_kFixed72, FW_kFixed72);
  428.  
  429.     FW_CRect bounds;
  430.     bitmap.GetBitmapBounds(bounds);
  431.     
  432.     FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, bounds);
  433.  
  434.     InitGraphicContext(fGraphicDevice,
  435.                        NULL,
  436.                        aqClipShape);
  437.  
  438.     FW_END_CONSTRUCTOR
  439. }                      
  440.  
  441. //----------------------------------------------------------------------------------------
  442. //    FW_CBitmapContext::~FW_CBitmapContext
  443. //----------------------------------------------------------------------------------------
  444.  
  445. FW_CBitmapContext::~FW_CBitmapContext()
  446. {
  447.     FW_START_DESTRUCTOR
  448. #ifdef FW_BUILD_MAC
  449.     fBitmap.MacUnlockPixels();
  450. #endif
  451.  
  452. #ifdef FW_BUILD_WIN
  453.     // Save the DC for later use
  454.     HDC hdcBitmap = FW_PrivGDev_GetPlatformCanvas(fGraphicDevice);
  455. #endif
  456.  
  457.     // ----- Terminate the superclass
  458.     TerminateGraphicContext();
  459.  
  460. #ifdef FW_BUILD_WIN
  461.     ::SelectObject(hdcBitmap, fOldBitmap);
  462.     ::DeleteDC(hdcBitmap);
  463. #endif
  464.  
  465.     // ----- Delete the device
  466.     FW_PrivGDev_Release(fGraphicDevice);
  467. }
  468.  
  469. //----------------------------------------------------------------------------------------
  470. //    FW_CBitmapContext::Reset
  471. //----------------------------------------------------------------------------------------
  472.  
  473. void FW_CBitmapContext::Reset()
  474. {
  475.     // Nothing to do [HLX] Am i sure ?
  476. }
  477.